using CodeConsult.Nordic.nRF8001.ApplicationControlInterface.Commands;
using CodeConsult.Nordic.nRF8001.ApplicationControlInterface.Commands.SystemCommands;

namespace CodeConsult.Nordic.nRF8001.ApplicationControlInterface.Events.CommandResponseEvents
{
    public class CommandResponseEvent : AciEventBase
    {
        public const byte OpCode = 0x84;
 
        public byte CommandOpCode { get; set; }
        public byte Status { get; set; }
        public byte[] ResponseData { get; set; }
        public AciCommandReponseBase CommandResponse { get; set; }

        protected override void DecodeFromBuffer()
        {
            var responseDataLen = DecodeHelper.BufferLength - 3;
            CommandOpCode = DecodeHelper.GetByte();
            Status = DecodeHelper.GetByte();
            ResponseData = DecodeHelper.GetBytes(responseDataLen);
        }

        public bool DecodeCommandResponse()
        {
            if (CommandResponse != null)
                return true;

            switch (CommandOpCode)
            {
                case GetDeviceVersionCommand.OpCode:
                    CommandResponse = new GetDeviceVersionCommandResponse();
                    break;
            }

            CommandResponse?.Decode(ResponseData);

            return CommandResponse != null;
        }

        public override string ToString()
        {
            return base.ToString() + " Opcode: " + OpCode + " Status: " + Status;
        }
    }
}
